#pragma once
//#include "stdafx.h"
#include "SimulationTime.h"
#include "RationalNumber.h"
namespace org
{
	namespace spark
	{
		namespace math
		{

			SimulationTime::SimulationTime(long long tick, RationalNumber *time)
			{
				init(tick, time, System::currentTimeMillis());
			}

			void SimulationTime::init(long long tick, RationalNumber *time, long long startTime, long long tickTime, double elapsedTime)
			{
				this->tick = tick;
				this->time = new RationalNumber(time);

				this->startTime = startTime;
				this->tickTime = tickTime;
				this->elapsedTime = elapsedTime;
			}

			void SimulationTime::init(long long tick, RationalNumber *time, long long startTime)
			{
				init(tick, time, startTime, startTime, 0);
			}

			SimulationTime::SimulationTime(SimulationTime *time)
			{
				init(time->tick, time->time, time->startTime, time->tickTime, time->elapsedTime);
			}

			SimulationTime::SimulationTime()
			{
			}

			void SimulationTime::reset()
			{
				init(0, new RationalNumber(0), System::currentTimeMillis());
			}

			long long SimulationTime::getTick()
			{
				return tick;
			}

			double SimulationTime::getElapsedTime()
			{
				return elapsedTime;
			}

			double SimulationTime::getTotalTime()
			{
				return (tickTime - startTime) * 0.001;
			}

			double SimulationTime::getDoubleTime()
			{
				return time;
			}

			RationalNumber *SimulationTime::getTime()
			{
				return new RationalNumber(time);
			}

			void SimulationTime::advanceTick()
			{
				tick++;

				// TODO: implement this correctly
				// Pausing/resuming should be taken into account (maybe)
				// Measure the speed of System.currentTimeMillis()
						/*		long curTime = System.currentTimeMillis();
								if (curTime > tickTime) {
									elapsedTime = (curTime - tickTime) * 0.001;
								}
								else {
									elapsedTime = 0.0;
								}
								
								tickTime = curTime;*/
			}

			void SimulationTime::setTime(RationalNumber *t)
			{
				time = new RationalNumber(t);
			}

			int SimulationTime::compareTo(RationalNumber *t)
			{
				return time->compare(t);
			}
		}
	}
}
